<template>
    <el-dialog :title="data.title" :visible.sync="dialog" width="500px">
            <el-form label-width="150px" :model="ruleForm" ref="ruleForm">
                <el-row>
                    <el-col v-for=" item in conditionD.list" :span="item.size" :key="item.model">
                        <el-form-item :label="item.label" :prop="item.model"  :rules="item.rules">
                            <el-input v-if="item.type=='input'" clearable
                                      :placeholder="item.placeholder"
                                      v-model="ruleForm[item.model]"></el-input>
                            <el-date-picker
                                v-if="item.type=='data'"
                                v-model="ruleForm[item.model]"
                                type="date"
                                :placeholder="item.placeholder"
                                value-format="yyyy-MM-dd"
                            >
                            </el-date-picker>
                            <el-select v-if="item.type=='select'&&(typeof(item.list)!='string')" v-model="ruleForm[item.model]">
                                <el-option
                                    v-for="i in item.list"
                                    :key="i.code"
                                    :label="i.label"
                                    :value="i.code">
                                </el-option>
                            </el-select>
                            <el-switch v-if="item.type=='switch'" v-model="ruleForm[item.model]"></el-switch>
                            <el-checkbox-group v-model="ruleForm[item.model]" v-if="item.type=='checkbox'" @change="change">
                                <el-checkbox v-for="i in item.list"
                                             :key="i.code"
                                             :label="i.code">{{i.label}}
                                </el-checkbox>
                            </el-checkbox-group>
                            <el-radio-group v-model="ruleForm[item.model]" v-if="item.type=='radio'">
                                <el-radio v-for="i in item.list"
                                          :key="i.code"
                                          :label="i.code">{{i.label}}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <div class="center-btn">
                            <el-button plain size="small" @click="closeDialog = false">取消</el-button>
                            <el-button type="primary" size="small" @click="submitClose('ruleForm')">确定</el-button>
                        </div>
                    </el-col>
                </el-row>
        </el-form>
    </el-dialog>
</template>

<script>
    export default {
        name: "dialog-submit",
        inject: ['data','row'],
        data() {
            return {
                dialog: false,
                ruleForm:{},
                conditionD:{}
            }
        },
        mounted(){
            // this.init()
        },
        watch:{
            data:{
                deep:true,
                handler(){
                    this.init()
                }
            },
        },
        methods:{
            change(){
                this.$forceUpdate()
            },
            init() {
                this.data.list.forEach( async item => {
                    if (item.url) {
                        //判断是否需要调接口
                        const res = await this.http[item.url.method](this.api[item.url.module][item.url.value])
                        if (res && res.code == 200) {
                            item.list = res.data.list
                        }
                        else {
                            this.$message.error(res.message)
                        }
                    }
                    if(item.type=='checkbox'){
                        this.ruleForm[item.model] = []
                    }
                    this.$forceUpdate()
                })
                Object.assign(this.conditionD,this.data)
            },
            submitClose(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(this.ruleForm)
                    } else {
                        this.$message.error('数据格式不对');
                        return false;
                    }
                })

            }
        }
    }
</script>

<style scoped>
 /deep/ .el-form-item__label{
     width: auto !important;
 }
 /deep/  .el-form-item__content{
     margin-left: 65px !important;
 }
</style>
